package com.yuke.cloud.service.cmc.redis;

import com.alibaba.fastjson.JSON;
import com.yuke.cloud.common.base.dto.MsgNotificationBody;
import com.yuke.cloud.common.util.RedisKeyUtil;
import com.yuke.cloud.service.api.opc.service.OpcSendNotifyFeignApi;
import com.yuke.cloud.service.cmc.entity.CmsRedReceive;
import com.yuke.cloud.service.cmc.service.CmsRedReceiveService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * <p>Company: Copyright (c) 2018. yuke All Rights Reserved. </br> </p>
 * <p>Title：yukemall </br> </p>
 * <p>com.yuke.cloud.service.cmc.redis Description TODO </br> </p>
 * @Author wg
 * @Date   2019/6/6 9:14
 */
@Component
public class RedisMessageListener implements MessageListener {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private OpcSendNotifyFeignApi opcSendNotifyFeignApi;
    @Resource
    private CmsRedReceiveService cmsRedReceiveService;


    @Override
    //key 过期时调用
    public void onMessage(Message message, byte[] pattern) {

        String strMessage = message.toString();
        System.out.println("过期事件："+strMessage);
        if (strMessage.startsWith("yuke-cloud:sec-kill:") && strMessage.endsWith("_id")) {
            // 删除对应的缓存
            // 删除商品及用户购买信息
            String strSecKillId = strMessage.substring("yuke-cloud:sec-kill:".length(), strMessage.length() - "_id".length());
            Set<String> keys = redisTemplate.keys(RedisKeyUtil.getSecKillSkuPrexKey(strSecKillId));
            if (CollectionUtils.isNotEmpty(keys)) {
                redisTemplate.delete(keys);
            }
        }else if (strMessage.startsWith("redReceive:")){
            String[] split = message.toString().split(":");
            String s1 = split[1];
            Long redReceiveId = Long.parseLong(s1);
            CmsRedReceive cmsRedReceive = cmsRedReceiveService.findById(redReceiveId);
            if(cmsRedReceive == null){
                return;
            }

           //设置过期时间
            redisTemplate.opsForValue().set("redReceiveExpire:"+redReceiveId,cmsRedReceive,5,TimeUnit.SECONDS);
            //通知客户

            String  notifyMsg = "您有一个通用红包即将过期，请在一个小时内使用！";
            MsgNotificationBody msg = new MsgNotificationBody();
            msg.setTitle("红包即将过期");
            msg.setContent(notifyMsg);
            msg.setTime(new Date());
            msg.setOrderType((byte) 1);
            msg.setMsgType("redReceiveMsg");
            msg.setReceiveType((byte) 3);
            Long userId = cmsRedReceive.getUserId();

            opcSendNotifyFeignApi.sendnotify(userId.toString(), "红包即将过期通知", notifyMsg, 0);
            opcSendNotifyFeignApi.sendnotify(userId.toString(), "红包即将过期通知", JSON.toJSONString(msg), 1);
        }else if (strMessage.startsWith("redReceiveExpire")){
            String[] split = message.toString().split(":");
            String s1 = split[1];
            Long redReceiveId = Long.parseLong(s1);
            cmsRedReceiveService.updateRedReceiveExpire(redReceiveId);
        }
    }
}
